<template>
  <el-dialog width="80%" v-drag title="新增商品标价签" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :inline="true" :model="dataForm" ref="dataFormRef" @keyup.enter.native="getDataList(1)">
      <el-form-item>
        选择过滤：
        <el-select size="mini" v-model="dataForm.selectField" clearable placeholder="请选择" @change="getDataList(1)"
          @clear="getDataList(1)">
          <el-option v-for="item in selectFields" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select size="mini" v-model="dataForm.selectValue" clearable placeholder="请选择" @change="getDataList(1)"
          @clear="getDataList(1)">
          <el-option label="是" value="1"></el-option>
          <el-option label="否" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        库存/价格/毛利过滤：
        <el-select size="mini" v-model="dataForm.priceField" clearable placeholder="请选择" @change="getDataList(1)"
          @clear="getDataList(1)">
          <el-option v-for="item in priceFields" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select size="mini" v-model="dataForm.priceFormula" clearable placeholder="请选择" @change="getDataList(1)"
          @clear="getDataList(1)">
          <el-option v-for="item in priceFormulas" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input size="mini" v-model="dataForm.priceValue" clearable type="number" placeholder="库存/价格/毛利"
          @blur="getDataList(1)" @clear="getDataList(1)"></el-input>
      </el-form-item>
      <el-form-item>
        查询过滤：
        <el-select size="mini" v-model="dataForm.queryField" clearable placeholder="请选择" @change="getDataList(1)"
          @clear="getDataList(1)">
          <el-option v-for="item in queryFields" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input size="mini" v-model="dataForm.key" placeholder="参数名" clearable @blur="getDataList(1)"
          @clear="getDataList(1)"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="success" @click="getDataList(1)" icon="el-icon-search">查询</el-button>
        <el-button @click="resetForm" size="mini" type="warning" icon="el-icon-refresh-left">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="table-box">
      <div class="left-table-box">

        <el-button size="mini" v-if="isAuth('sale:print:save')" type="success"
          @click="addAllHandle()" icon="el-icon-plus" :disabled="leftDataListSelections.length <= 0">添加</el-button>

        <el-table :data="leftDataList" size="mini" v-loading="dataListLoading" @selection-change="leftSelectionChangeHandle"
          style="width: 100%;" @cell-click="tableDbEdit" @cell-dblclick="tableDbEdit"
          height="400px"
          :header-cell-style="{ background: '#fcfcfc', color: '#606266', height:'36px'}">
          <el-table-column type="selection" header-align="center" align="center" width="50">
          </el-table-column>
          <el-table-column prop="productCode" header-align="center" align="center" label="产品编码" width="130" >
          </el-table-column>
          <el-table-column prop="productName" header-align="center" align="center" label="产品名称" width="130" show-overflow-tooltip>
          </el-table-column>
          <el-table-column prop="standard" header-align="center" align="center" label="规格" width="130" show-overflow-tooltip>
          </el-table-column>
          <el-table-column prop="unit" header-align="center" align="center" label="单位">
          </el-table-column>
          <el-table-column prop="salePrice" header-align="center" align="center" label="售价" width="90">
          </el-table-column>
          <el-table-column v-if="false" prop="produceDate" header-align="center" align="center" label="生产日期" >
          </el-table-column>
          <el-table-column v-if="false" prop="expiryDate" header-align="center" align="center" label="有效期">
          </el-table-column>
          <el-table-column fixed="right" header-align="center" align="center" width="100" label="操作">
            <template slot-scope="scope">
              <el-button size="mini" type="text" v-if="isAuth('sale:print:save')"
                @click="addLeftHandle(scope.row, scope.$index)">添加</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
          layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
      </div>
      <div class="right-table-box">
        <el-button size="mini" v-if="isAuth('sale:print:delete')" type="danger"
          @click="deleteAllHandle()" icon="el-icon-delete" >清空</el-button>
        <el-table :data="rightDataList" size="mini" v-loading="dataListLoading"
          style="width: 100%;"  height="400px"
          :header-cell-style="{ background: '#fcfcfc', color: '#606266', height:'36px'}">
          <el-table-column prop="productCode" header-align="center" align="center" label="产品编码" width="130" >
          </el-table-column>
          <el-table-column prop="productName" header-align="center" align="center" label="产品名称" width="130" show-overflow-tooltip>
          </el-table-column>
          <el-table-column prop="standard" header-align="center" align="center" label="规格" width="130" show-overflow-tooltip>
          </el-table-column>
          <el-table-column prop="unit" header-align="center" align="center" label="单位">
          </el-table-column>
          <el-table-column prop="salePrice" header-align="center" align="center" label="售价" width="90">
          </el-table-column>
          <el-table-column v-if="false" prop="produceDate" header-align="center" align="center" label="生产日期" >
          </el-table-column>
          <el-table-column v-if="false" prop="expiryDate" header-align="center" align="center" label="有效期">
          </el-table-column>
         <el-table-column fixed="right" header-align="center" align="center" width="100" label="操作">
           <template slot-scope="scope">
             <el-button size="mini" type="text" v-if="isAuth('sale:print:delete')"
               @click="deleteRightHandle(scope.row, scope.$index)">删除</el-button>
           </template>
         </el-table-column>
        </el-table>
      </div>

    </div>

    <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="visible = false">取消</el-button>
      <el-button size="mini" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { Loading } from "element-ui"
  export default {
    data() {
      return {
        visible: false,
        dataForm: {
          selectField: '',
          selectValue: '',
          priceField: '',
          priceFormula: '',
          priceValue: '',
          queryField: '',
          key: '',
          unit: '',
          categoryId: ''
        },
        leftDataList: [],
        leftDataListSelections: [],
        rightDataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        selectFields: [],
        priceFields: [],
        queryFields: [],
        priceFormulas: [],
      }
    },
    mounted(){
      this.initData()
    },
    methods: {
      init(id) {
        this.rightDataList = []
        this.visible = true
        this.getDataList(0)
      },
      // 获取数据列表
      async getDataList(queryType) {
        this.dataListLoading = true
        const res = await this.$http({
          url: `/product/info/list`,
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'dataForm': this.dataForm,
            'queryType': (queryType ? queryType: 0)
          })
        })
        let data = res.data
        if (data && data.code === 0) {
          this.leftDataList = data.page.list
          this.totalPage = data.page.totalCount
          this.leftDataList.forEach(item => {
            item.status = item.status == 1 ? true : false
          })
        } else {
          this.leftDataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList(1)
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList(1)
      },
      // 多选
      leftSelectionChangeHandle(val) {
        this.leftDataListSelections = val
      },
      rightSelectionChangeHandle(val) {
        this.rightDataListSelections = val
      },
      resetForm() {
        this.$nextTick(() => {
          this.dataForm = this.$options.data().dataForm
          this.getDataList(0)
        })
      },
      tableDbEdit(row, column, event) {
      },
      addLeftHandle(row,index){
        this.rightDataList.push(row)
        this.leftDataList.splice(index, 1)
      },
      deleteRightHandle(row, index){
        this.leftDataList.push(row)
        this.rightDataList.splice(index, 1)
      },
      addAllHandle(){
        this.rightDataList.push.apply(this.rightDataList, this.leftDataListSelections)
        // for(let i = 0; i < this.leftDataListSelections.length; i++){
        //     for(let j = 0; j < this.leftDataList.length; j++){
        //         if(this.leftDataList[j].productId == this.leftDataListSelections[i].productId){
        //           this.leftDataList.splice(j,1);
        //           break;
        //         }
        //     }
        // }
        let arr = this.leftDataList
        let selectedArr = this.leftDataListSelections
        this.leftDataList = arr.filter(x => !selectedArr.some(y => y.productId  === x.productId ))
      },
      deleteAllHandle(){
        this.leftDataList.push.apply(this.leftDataList, this.rightDataList)
        this.rightDataList = []
      },
      initData() {
        let selectFields = [{
          value: 'is_rx_drug',
          label: '是否处方'
        }, {
          value: 'is_yi_bao',
          label: '是否医保'
        }, {
          value: 'is_drug',
          label: '是否中药'
        }, {
          value: 'is_split',
          label: '是否拆零药'
        }, {
          value: 'is_can_split',
          label: '能否拆零'
        }, {
          value: 'is_score',
          label: '是否积分'
        }, {
          value: 'is_discount',
          label: '是否折扣'
        }, {
          value: 'is_gift',
          label: '是否赠品'
        }, {
          value: 'is_commission',
          label: '是否提成'
        }, {
          value: 'is_first_sale',
          label: '是否首营'
        }];

        let priceFields = [{
          value: 'total_storage',
          label: '总库存'
        }, {
          value: 'profit',
          label: '毛利'
        }, {
          value: 'sale_price',
          label: '售价'
        }, {
          value: 'trade_price',
          label: '批发价'
        },{
          value: 'purchase_price',
          label: '采购价'
        }, {
          value: 'retail_price',
          label: '指导价'
        }, {
          value: 'member_price',
          label: '会员价'
        }, {
          value: 'discount_price',
          label: '折扣价'
        }, {
          value: 'top_price',
          label: '最高价'
        }, {
          value: 'lowest_price',
          label: '最低价'
        }];

        let priceFormulas = [{
          value: 'GE',
          label: '大于等于'
        }, {
          value: 'EQ',
          label: '等于'
        }, {
          value: 'GT',
          label: '大于'
        }, {
          value: 'LE',
          label: '小于等于'
        }, {
          value: 'LT',
          label: '小于'
        }, {
          value: 'NE',
          label: '不等于'
        }, ]

        let queryFields = [{
          value: 'product_code',
          label: '编码'
        }, {
          value: 'bar_code',
          label: '条形码'
        }, {
          value: 'product_name',
          label: '产品名称'
        }, {
          value: 'generic_name',
          label: '通用名'
        }, {
          value: 'pinyin',
          label: '助记码'
        }, {
          value: 'producing_area',
          label: '产地'
        }, {
          value: 'produce_factory',
          label: '厂家'
        }, {
          value: 'dosage_form',
          label: '剂型'
        }, {
          value: 'standard',
          label: '规格'
        }, {
          value: 'effect',
          label: '效果'
        }, {
          value: 'approval_no',
          label: '批准文号'
        }]

        this.selectFields = selectFields;
        this.priceFields = priceFields;
        this.priceFormulas = priceFormulas;
        this.queryFields = queryFields;
      },
      // 表单提交
      dataFormSubmit() {
        this.visible = false;
        let parentList = this.$parent.dataList
        let rightList = this.rightDataList;
        let newArr = [...parentList, ...rightList]
        let obj = {}
        this.$parent.dataList = newArr.reduce((item, next) => {
          obj[next.productId] ? '' : obj[next.productId] = true && item.push(next)
          return item
        }, [])
        sessionStorage.setItem("printDatas", JSON.stringify(this.$parent.dataList))
      }
    }
  }
</script>
<style lang="scss" scoped>
  .el-table{
    border: 1px solid #EBEEF5!important;
    border-radius: 5px;
  }
  .table-box{
    display: flex;
    flex-direction: row;
    justify-content: space-between;

    .left-table-box{
      width: 49.5%;
    }
    .right-table-box{
      width: 49.5%;
    }
  }

</style>
